/**
  ******************************************************************************
  * @file    User/tk_cfg.c
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    03-18-2022
  * @brief   TK configuration program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "tk_define.h"

/** @addtogroup Template_Project
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/


__attribute__((aligned(4))) u16 tk_buf[(TK_NUM+1) *17 + 14] = {
                (TK_BIT_FLAG(TK_1)<<8)|TK_BIT_FLAG(TK_0) , (TK_BIT_FLAG(TK_3)<<8)|TK_BIT_FLAG(TK_2) , (TK_BIT_FLAG(TK_5)<<8)|TK_BIT_FLAG(TK_4) , \
                (TK_BIT_FLAG(TK_7)<<8)|TK_BIT_FLAG(TK_6) , (TK_BIT_FLAG(TK_9)<<8)|TK_BIT_FLAG(TK_8) , \
                (TK_BIT_FLAG(TK_11)<<8)|TK_BIT_FLAG(TK_10) , (TK_BIT_FLAG(TK_13)<<8)|TK_BIT_FLAG(TK_12) , (TK_BIT_FLAG(TK_15)<<8)|TK_BIT_FLAG(TK_14) , \
                (TK_BIT_FLAG(TK_17)<<8)|TK_BIT_FLAG(TK_16) , (TK_BIT_FLAG(TK_19)<<8)|TK_BIT_FLAG(TK_18) , \
                (TK_BIT_FLAG(TK_21)<<8)|TK_BIT_FLAG(TK_20) , (TK_BIT_FLAG(TK_23)<<8)|TK_BIT_FLAG(TK_22) , (TK_BIT_FLAG(TK_25)<<8)|TK_BIT_FLAG(TK_24) , \
                TK_THRE_ALL PRESETBUF  };

#if TK_SWD_DEBUG_EN
u16 tk_swd_buf[TK_NUM];
//u16 tk_swd_buf[26];
#endif



/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


void tk_init(void)
{
    TYPE_LL_GPIO_INIT        gpio_struct;

    memset(&gpio_struct, 0x0, sizeof(gpio_struct));
    gpio_struct.gpio_pin   =  (TK_BIT_EN & 0x3fffc00)>>10;
    gpio_struct.gpio_mode  = LL_GPIO_MODE_OUT;
    gpio_struct.gpio_speed_level = LL_GPIO_SPEED_LEVEL_0;
    ll_gpio_init(GPIOA,&gpio_struct);//PA

    memset(&gpio_struct, 0x0, sizeof(gpio_struct));
    gpio_struct.gpio_pin   =  (TK_BIT_EN & 0x3ff);
    gpio_struct.gpio_mode  = LL_GPIO_MODE_OUT;
    gpio_struct.gpio_speed_level = LL_GPIO_SPEED_LEVEL_0;
    ll_gpio_init(GPIOB,&gpio_struct);//PB

    ll_gpio_reset_bits(GPIOA, (TK_BIT_EN & 0x3fffc00)>>10);
    ll_gpio_reset_bits(GPIOB, (TK_BIT_EN & 0x3ff));
    
    TK_StructDef tk_struct;
    tk_struct.buf = (u32)tk_buf;
    tk_struct.tk_bit_en = ((u32)TK_NUM<<27) | TK_BIT_EN;
    tk_struct.systick = (u32)&SysTickCnt; 
    
#if TK_SWD_DEBUG_EN
    tk_struct.swd_buf = (u32)tk_swd_buf;
#else
    tk_struct.swd_buf = 0;
#endif

    TK_ProcDef tk_proc;
    tk_proc.lwfltr_value = 0;//<256
    tk_proc.two_keys_proc = 0;
    tk_proc.multi_keys_proc = 0;
    tk_proc.filter_cnt = 1; //<16
    tk_proc.bl_update_level = 1; //<16
    tk_proc.hold_dowm_release_time = 0; //<8  0: 10s, 1: 20s,...,7:80s
    
    if(tk_struct_init((TK_StructDef *)(&tk_struct),(TK_ProcDef *)(&tk_proc)))
        return;           //1:TK_NUM != {TK_0,TK_1,...,};

    if(tk_ctrl_init())
    {
        for(u8 i=0; i<3; i++)
        {
            //delay_ms(100);
            if(!tk_ctrl_init())
                return;
        }
    }
}

void IO_wkup_set()
{
	/*
   TYPE_LL_GPIO_INIT        gpio_struct;
   memset(&gpio_struct, 0x0, sizeof(gpio_struct));
   gpio_struct.gpio_pin   =  LL_GPIO_PIN_12;
   gpio_struct.gpio_mode  = LL_GPIO_MODE_AF;
   gpio_struct.gpio_pupd = LL_GPIO_PUPD_DOWN;
   ll_gpio_init(GPIOA,&gpio_struct);//PA
   ll_gpio_pin_af_config(GPIOA, LL_GPIO_PIN_SOURCE_12, LL_GPIO_AF_0);
 
    SYSCTRL_REG_OPT(
        SYSCTRL->SYS_CON0 = (SYSCTRL->SYS_CON0 & (~LL_SYSCTRL_CON0_SLEEP_DLY_CNT(ALL_WORD_FF))) 
                                    | LL_SYSCTRL_CON0_SLEEP_DLY_CNT(0x5);
        SYSCTRL->WKUP_CON0 = 0xF << 24;
        SYSCTRL->WKUP_CON0 |= 0x1 << 1;
    );
	*/	
}

void tk_lp_init()
{

    //IO
    TYPE_LL_GPIO_INIT        gpio_struct;

    memset(&gpio_struct, 0x0, sizeof(gpio_struct));
    gpio_struct.gpio_pin   =  (u16)((~TK_BIT_EN & 0x3fffc00)>>10);  //except PA14 | PA15
    gpio_struct.gpio_mode  = LL_GPIO_MODE_AN;
    gpio_struct.gpio_speed_level = LL_GPIO_SPEED_LEVEL_0;
    //gpio_struct.gpio_pupd = LL_GPIO_PUPD_DOWN;
    ll_gpio_init(GPIOA,&gpio_struct);//PA

    memset(&gpio_struct, 0x0, sizeof(gpio_struct));
    gpio_struct.gpio_pin   =  (~TK_BIT_EN) & 0x3fff ;
    gpio_struct.gpio_mode  = LL_GPIO_MODE_AN;
    gpio_struct.gpio_speed_level = LL_GPIO_SPEED_LEVEL_0;
    //gpio_struct.gpio_pupd = LL_GPIO_PUPD_DOWN;
    ll_gpio_init(GPIOB,&gpio_struct);//PB

    IO_wkup_set();
    //clk
    SYSCTRL_REG_OPT(
        SYSCTRL->CLK_CON0  = (SYSCTRL->CLK_CON0 & (~LL_SYSCTRL_CLK_CON0_SYSCLK_SEL(ALL_WORD_FF))) 
                            | LL_SYSCTRL_CLK_CON0_SYSCLK_SEL(0x0);  //sel 256k
        SYSCTRL->CLK_CON3 &= ~LL_SYSCTRL_CLK_CON3_HIRC_EN;        //disable HIRC_OSC 
    );

    tk_lp_ctrl_init();  // after clk

    //pmu
    SYSCTRL_REG_OPT(
        SYSCTRL->PMU_CON0 &= ~(LL_SYSCTRL_PMU_CON0_HP_LDO_EN | LL_SYSCTRL_PMU_CON0_HP_HPV2I_EN | LL_SYSCTRL_PMU_CON0_INNER_LDO_EN);
        SYSCTRL->PMU_CON0 &= ~(LL_SYSCTRL_PMU_CON0_HP_PDI_EN | LL_SYSCTRL_PMU_CON0_HP_PDLI_EN);
        SYSCTRL->PMU_CON0 |= LL_SYSCTRL_PMU_CON0_LP_IDEEP;
    );
     
    tk_wakeup(0,0);

    //pmu
    SYSCTRL_REG_OPT(   
        SYSCTRL->PMU_CON0 |= (LL_SYSCTRL_PMU_CON0_HP_LDO_EN | LL_SYSCTRL_PMU_CON0_HP_HPV2I_EN | LL_SYSCTRL_PMU_CON0_INNER_LDO_EN);
        SYSCTRL->PMU_CON0 |= (LL_SYSCTRL_PMU_CON0_HP_PDI_EN | LL_SYSCTRL_PMU_CON0_HP_PDLI_EN);
        SYSCTRL->PMU_CON0 &= ~LL_SYSCTRL_PMU_CON0_LP_IDEEP;
    );
	
    //clk
    sys_clk_init();
}



/**
  * @}
  */

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
